<template>
  <el-container class="home-container">
    <!-- 头部内容 -->
    <el-header>
      <div class="header-left">
        <img src="../assets/images/logo.png" alt="logo" />
        <span>电商后台管理系统</span>
      </div>
      <el-button class="logout" type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体内容 -->
    <el-container>
      <!-- 左侧菜单内容 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠（展开）按钮 -->
        <div
          class="toggle-btn"
          :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
          @click="toggleCollapse"
        ></div>

        <!-- 菜单栏 -->
        <el-menu
          router
          unique-opened
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EFF"
          :default-active="activePath"
          :collapse="isCollapse"
          :collapse-transition="false"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span slot="title">{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边主体内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <!-- @changeNavState:监听子组件中名为 changeNavState 的自定义方法 -->
        <router-view @changeNavState="saveNavState" />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 控制左侧菜单栏是否折叠
      isCollapse: false,
      // 被激活菜单链接地址
      activePath: '/users',
      // 菜单列表列表
      menuList: [],
      // 分类图标对象
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      }
    }
  },
  created() {
    // 调用获取左侧菜单栏数据方法
    this.getMenuList()
    // 当用户刷新时选中对应组件的左侧菜单
    this.saveNavState(window.sessionStorage.getItem('activePath'))
  },
  methods: {
    // 退出方法
    logout() {
      // 清空 sessionStorage
      window.sessionStorage.clear()
      // 跳转到登录页面
      this.$router.push('/login')
      this.$message('退出登录')
    },
    // 获取左侧菜单栏数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 切换折叠和展开的方法
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 点击二级分类菜单时保存该分类
    saveNavState(activePath) {
      // 保存被激活的链接地址
      window.sessionStorage.setItem('activePath', activePath)
      // 被激活链接地址
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #353c3f;

  .header-left {
    height: 100%;
    font-size: 24px;
    color: #fff;

    > img {
      height: 100%;
    }

    > span {
      display: inline-block;
      margin-top: 14px;
      margin-left: 10px;
      vertical-align: top;
    }
  }

  .logout {
    position: absolute;
    top: 10px;
    right: 10px;
  }
}

.el-aside {
  background-color: #434a50;
  transition: all 0.2s;

  .toggle-btn {
    width: 100%;
    text-align: center;
    color: #fff;
    cursor: pointer;
  }

  .el-menu {
    border: none;
  }

  .iconfont {
    margin: 0 5px;
  }
}

.el-main {
  margin: 0;
  padding: 0;
  background-color: #e9edf1;
}
</style>
